<template>
  <div id="Roles">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col class="addBtn">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList" style="width: auto" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
            :class="['col_center', 'bd_bottom', index1 === 0 ? 'bd_top' : '']"
            v-for="(item1, index1) in scope.row.children"
            :key="item1.id"
            >
              <el-col :span="5">
                <el-tag
                closable
                @close="showDelDialog(scope.row.id, item1.id)"
                >{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row :class="['col_center', index2 === 0 ? '' : 'bd_top']" v-for="(item2, index2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="showDelDialog(scope.row.id, item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                    type="warning"
                    v-for="(item3) in item2.children"
                    closable :key="item3.id"
                    @close="showDelDialog(scope.row.id, item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showRightDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="提示" :visible.sync="delRightDialogVisible" width="30%">
      <span style="color: red">您确定删除此项权限吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delRoles">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="分配权限" :visible.sync="setRightDialogViisble" width="40%">
      <el-tree
      :data="rightsList"
      :props="defaultProps"
      show-checkbox node-key="id"
      :default-expand-all="true"
      :default-checked-keys="defKeys"
      ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogViisble = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      rolesList: [],
      rightsList: [],
      delRightDialogVisible: false,
      setRightDialogViisble: false,
      delInfo: {},
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      role_Id: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    getRolesList () {
      this.$http.get('/roles').then(ret => {
        this.rolesList = ret.data.data
        console.log(this.rolesList)
      }, () => {
        this.$message.error('获取角色列表失败')
      })
    },
    showDelDialog (roleId, rightId) {
      this.delInfo = { roleId, rightId }
      console.log(this.delInfo)
      this.delRightDialogVisible = true
    },
    delRoles () {
      this.$http.delete(`/roles/${this.delInfo.roleId}/rights/${this.delInfo.rightId}`).then(ret => {
        this.$message.success('删除权限成功')
        this.delRightDialogVisible = false
        this.getRolesList()
      }, () => {
        this.$message.error('删除权限失败')
      })
    },
    showRightDialog (roles) {
      this.$http.get('/rights/tree').then(ret => {
        this.rightsList = ret.data.data
        console.log(roles)
        this.role_Id = roles.id
        this.defKeys = []
        this.getLeafKeys(roles, this.defKeys)
        this.setRightDialogViisble = true
      }, () => {
        this.$message.error('获取权限列表失败')
      })
    },
    getLeafKeys (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    allotRights () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const keyStr = keys.join(',')
      console.log(keyStr)
      this.$http.post(`roles/${this.role_Id}/rights`, { rids: keyStr }).then(ret => {
        this.getRolesList()
        this.setRightDialogViisble = false
        this.$message.success('分配权限成功')
      }, () => {
        this.$message.error('分配权限失败')
      })
    }
  }
}
</script>

<style Lang="less" scoped>
  .el-card{
    margin-top: 15px;
  }
  .addBtn{
    display: flex;
    justify-content: flex-start;
  }
  .el-table{
    margin-top: 12px;
  }
  .el-tag{
    margin: 7px;
  }
  .bd_top{
    border-top: 1px solid #eee;
  }
  .bd_bottom{
    border-bottom: 1px solid #eee;
  }
  .col_center {
    display: flex;
    align-items: center;
  }
</style>
